package com.wealth.vouchermarket.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Category;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.enums.GoodsStatusEnum;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.merchant.entity.Merchant;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.merchant.service.IMerchantService;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

/**
 * 惠生活 - 首页
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/vouchermarket/index")
public class VoucherMarketIndexController extends BaseController{

	Logger logger = Logger.getLogger(VoucherMarketIndexController.class);
	
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IMerchantService merchantService;
	@Autowired
	private IGoodsService goodsService;
	
	/** 
	 * 首页
	 */
	@RequestMapping
	public String index(Model model,HttpServletRequest request) {
		try{
			//TODO
			//父分类
			List<Category> categorytList = categoryService.getCategoryByCorpIdAndMarketIdAndStatusAndParentIdIsNull(1L, MarketEnum.LIFE.getValue(),  StatusEnum.NORMAL.getValue());
			model.addAttribute("categorytList", categorytList);
			
			//商户
			List<Merchant> merchantList = merchantService.findByMarketIdAndStatus(MarketEnum.LIFE.getValue(),  StatusEnum.NORMAL.getValue());
			model.addAttribute("merchantList", merchantList);

			//商品(销量查询limit5)
			List<Goods> goodsList = goodsService.getGoodsByMerchantMarketIdAndStatus( MarketEnum.LIFE.getValue(),  GoodsStatusEnum.UP.getValue());
			model.addAttribute("goodsList", goodsList);
			
			return "vouchermarket/index";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	/** 
	 * 商户列表
	 */
	@RequestMapping(value="/ajaxHuiLife",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject list(@RequestParam String name) {
		try{
			
			//商品(销量查询limit5)
			List<Goods> goodsList = goodsService.ajaxHuiLife(MarketEnum.LIFE.getValue(), GoodsStatusEnum.UP.getValue(), name);
			return new ResultObject("000", "商户列表加载成功",goodsList);
			
		}catch(Exception e){
			return new ResultObject("999", "商户列表加载错误", "");
		}
	}
	
	
}
